package com.xy.p2p.action.admin;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.web.CookieUtils;
import com.xy.core.web.WebErrors;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Tuser;
import com.xy.p2p.model.Twidrapp;
import com.xy.p2p.service.TmemberService;
import com.xy.p2p.service.TmemberassetaccountdetailsService;
import com.xy.p2p.service.TwidrappService;



@Controller
public class TwidrappAction{
	private static final Logger log = LoggerFactory.getLogger(TwidrappAction.class);
	public TwidrappAction() {
	}
 
 
 	/**
	 * 提现申请表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_twidrappAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("twidrapp", new Twidrapp());
        setDict(model);
		return "twidrappEdit";
	}

	/**
	 * 提现申请表新增保存
	 * 
	 * @param twidrapp
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_twidrappSave.do")
	public String save(Twidrapp twidrapp, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(twidrapp, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		twidrapp = twidrappService.save(twidrapp);
		return "redirect:v_twidrappList.do";
	}

	/**
	 * 提现申请表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_twidrappDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				twidrappService.remove(id);
			}
		}
		return "redirect:v_twidrappList.do";
	}

	/**
	 * 提现申请表编辑页面
	 * 
	 * @param waid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_twidrappEdit.do")
	public String edit(long waid, HttpServletRequest request, ModelMap model) {
		Twidrapp twidrapp = twidrappService.get(waid);
		model.addAttribute("twidrapp", twidrapp);
        setDict(model);
		return "twidrappEdit";
	}
	
	@RequestMapping("v_twidrappAudit.do")
	public String audit(long waid, HttpServletRequest request, ModelMap model) {
		Twidrapp twidrapp = twidrappService.get(waid);
		model.addAttribute("twidrapp", twidrapp);
		model.addAttribute("opr", "audit");
        setDict(model);
		return "twidrappView";
	}
	
	@RequestMapping("v_twidrappAuditSubmit.do")
	public String auditSubmit(long waid,String opr, HttpServletRequest request, ModelMap model) {
		Twidrapp twidrapp = twidrappService.get(waid);
		twidrappService.audit( (Tuser)request.getSession().getAttribute(TuserAction.AUTH_KEY), opr, twidrapp);
		return "redirect:v_twidrappList.do";
	}

	/**
	 * 提现申请表详情
	 * 
	 * @param waid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_twidrappDetail.do")
	public String detail(long waid, HttpServletRequest request, ModelMap model) {
		Twidrapp twidrapp = twidrappService.get(waid);
		model.addAttribute("twidrapp", twidrapp);
        setDict(model);
		return "twidrappView";
	}

	/**
	 * 提现申请表查询（分页）
	 * 
	 * @param twidrapp
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_twidrappList.do")
	public String listPage(Twidrapp twidrapp, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = twidrappService.getTwidrappByPage(toMap(twidrapp),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "twidrappList";
	}

	/**
	 * 提现申请表查询
	 * 
	 * @param twidrapp
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_twidrappList1.do")
	public String list(Twidrapp twidrapp, HttpServletRequest request, ModelMap model) {
		List<Twidrapp> list = twidrappService.getTwidrapp(toMap(twidrapp));
		model.addAttribute("list", list);
        setDict(model);
		return "twidrappList";
	}
//	/*
//	 * 查询
//	 * */
//	@RequestMapping("v_twidrappList2.do")
//	public String list2(Twidrapp twidrapp, HttpServletRequest request, ModelMap model) {
//		List<Twidrapp> list = twidrappService.getTwidrapp(toMap(twidrapp));
//		model.addAttribute("list", list);
//        setDict(model);
//		return "twidrappInfo";
//	}
	/*
	 * 提现中
	 * */
	@RequestMapping("v_twidrappList3.do")
	public String list3(Twidrapp twidrapp, HttpServletRequest request, ModelMap model) {
		twidrapp = new Twidrapp();
		twidrapp.setStatus(2);
		List<Twidrapp> list = twidrappService.getTwidrapp(toMap(twidrapp));
		model.addAttribute("list", list);
        setDict(model);
		return "twidrappInfo";
	}

 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param twidrapp
	 * @return
	 */	private Map<String,Object> toMap(Twidrapp twidrapp){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(twidrapp.getWaid()!=null&&!twidrapp.getWaid().equals("")){
			map.put("waid", twidrapp.getWaid());
		}
		if(twidrapp.getTmember()!=null&&!twidrapp.getTmember().equals("")){
			map.put("Tmember", twidrapp.getTmember());
		}
		if(twidrapp.getWdam()!=null&&!twidrapp.getWdam().equals("")){
			map.put("wdam", twidrapp.getWdam());
		}
		if(twidrapp.getWdamdate()!=null&&!twidrapp.getWdamdate().equals("")){
			map.put("wdamdate", twidrapp.getWdamdate());
		}
		if(twidrapp.getTuser()!=null&&!twidrapp.getTuser().equals("")){
			map.put("reviewed", twidrapp.getTuser());
		}
		if(twidrapp.getRevieweddate()!=null&&!twidrapp.getRevieweddate().equals("")){
			map.put("revieweddate", twidrapp.getRevieweddate());
		}
		if(twidrapp.getOperator()!=null&&!twidrapp.getOperator().equals("")){
			map.put("operator", twidrapp.getOperator());
		}
		if(twidrapp.getOperatedate()!=null&&!twidrapp.getOperatedate().equals("")){
			map.put("operatedate", twidrapp.getOperatedate());
		}
		if(twidrapp.getStatus()!=null&&!twidrapp.getStatus().equals("")){
			map.put("status", twidrapp.getStatus());
		}
		if(twidrapp.getRemark()!=null&&!twidrapp.getRemark().equals("")){
			map.put("remark", twidrapp.getRemark());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TWIDRAPP_STATUS));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Twidrapp twidrapp, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TwidrappService twidrappService;   
	@Resource
	private TmemberassetaccountdetailsService tmemberassetaccountdetailsService;
	@Resource
	private TmemberService tmemberService;
}